package com.lulu.baselib.util;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Author: XuChang
 * Date: 2019/10/26 21:59
 * Email: 123286473@qq.com
 * Description:
 * History:
 */
public class ImageUtil {


    public interface LoadImageCallback {
        void onLoadSuccess(Bitmap bitmap);

        void onLoadFailed(String msg);
    }

    /**
     * 加载网络图片
     * @param url
     * @param loadImageCallback 回调对象
     */
    public static void loadImage(String url, LoadImageCallback loadImageCallback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            public void onFailure(Call call, IOException e) {
                loadImageCallback.onLoadFailed(e.getMessage());
            }

            public void onResponse(Call call, Response response) throws IOException {
                InputStream inputStream = response.body().byteStream();//得到图片的流
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                if (bitmap != null) {
                    loadImageCallback.onLoadSuccess(bitmap);
                } else {
                    loadImageCallback.onLoadFailed("加载图片失败");
                }
            }
        });
    }



    /**
     * 从uri中获取bitmap
     * @param mContext
     * @param uri
     * @param reqWidth
     * @param reqHeight
     * @return
     * @throws IOException
     */
    public static Bitmap getThumbnail(Activity mContext, Uri uri, int reqWidth, int reqHeight) throws IOException {
        InputStream input = mContext.getContentResolver().openInputStream(uri);
        BitmapFactory.Options options =new BitmapFactory.Options();
        options.inJustDecodeBounds=true;
        BitmapFactory.decodeStream(input, null,options);
        if((options.outWidth== -1) || (options.outHeight== -1))
            return null;
        options.inSampleSize=calculateInSampleSize(options,reqWidth,reqHeight);
        options.inJustDecodeBounds=false;
        input.close();
        input = mContext.getContentResolver().openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(input, null,options);
        input.close();
        return bitmap;
    }

    private static int calculateInSampleSize(BitmapFactory.Options options,
                                             int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }
}
